/*
** Copyright (C) 2010 Calin Radoni
**
** This file is part of CHScanner.
**
** CHScanner is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** CHScanner is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with CHScanner. If not, see <http://www.gnu.org/licenses/>.
*/

#include "StdAfx.h"
#include "MGxRPDecoder.h"

//---------------------------------------------------------------------------

MGxRPDecoder::MGxRPDecoder(void)
{
}

MGxRPDecoder::~MGxRPDecoder(void)
{
}

//---------------------------------------------------------------------------

GxRPType MGxRPDecoder::GetProtocolType(MGxRPPacket* packIn)
{
	if(packIn==NULL) return __gxrpNull;

	return packIn->ProtocolType;
}

CString MGxRPDecoder::GetProtocolName(MGxRPPacket* packIn)
{
	CString str;

	if(packIn==NULL){
		str.Empty();
		return str;
	}

	switch(packIn->ProtocolType){
		case __gxrpGvrp :
			if(packIn->ProtocolID==0x0001) str = _T("GARP VLAN Registration Protocol");
			else                           str.Format(_T("- GVRP unknown: %d -"), packIn->ProtocolID);
			break;

		case __gxrpGmrp :
			str = _T("GARP Multicast Registration Protocol");
			break;

		default:
			str.Format(_T("- unknown: %u -"), packIn->ProtocolID);
			break;
	}

	return str;
}

//---------------------------------------------------------------------------

CString MGxRPDecoder::GetAttributeTypeNameGVRP(MGxRPPacket* packIn)
{
	CString str;

	switch(packIn->AttributeType){
		case 0x01:
			str = _T("VID attribute");
			break;
		default:
			str.Format(_T("- unknown (%u)-"), packIn->AttributeType);
			break;
	}

	return str;
}

CString MGxRPDecoder::GetAttributeTypeNameGMRP(MGxRPPacket* packIn)
{
	CString str;

	switch(packIn->AttributeType){
		case 0x01:
			str = _T("Group attribute");
			// followed by a MAC ?
			break;
		case 0x02:
			str = _T("Service Requirement attribute");
			// followed by one byte (0==All Groups, 1==All Unregistered Groups, others=undefined)
			break;
		default:
			str.Format(_T("- unknown (%u)-"), packIn->AttributeType);
			break;
	}

	return str;
}

CString MGxRPDecoder::GetAttributeTypeName(MGxRPPacket* packIn)
{
	CString str;

	if(packIn==NULL){
		str.Empty();
		return str;
	}

	switch(packIn->ProtocolType){
		case __gxrpGvrp:
			return GetAttributeTypeNameGVRP(packIn);

		case __gxrpGmrp:
			return GetAttributeTypeNameGMRP(packIn);

		default:
			str.Format(_T("%u (invalid protocol type %u)"), packIn->AttributeType, packIn->ProtocolType);
			return str;
	}
}

//---------------------------------------------------------------------------

CString MGxRPDecoder::GetAttributeEventName(BYTE attrEvtIn)
{
	CString str;

	switch(attrEvtIn){
		case 0x00:
			str = _T("Leave All");
			break;
		case 0x01:
			str = _T("Join Empty");
			break;
		case 0x02:
			str = _T("Join In");
			break;
		case 0x03:
			str = _T("Leave Empty");
			break;
		case 0x04:
			str = _T("Leave In");
			break;
		case 0x05:
			str = _T("Empty");
			break;
		default:
			str.Format(_T("- unknown (%u)-"), attrEvtIn);
			break;
	}

	return str;
}

//---------------------------------------------------------------------------

CString MGxRPDecoder::GetAttributeValueGVRP(MGxRPPacket* packIn, BYTE xLenIn, BYTE xVal0, BYTE xVal1)
{
	CString str;
	WORD wVal;

	if(packIn->ProtocolID!=0x0001){
		str.Format(_T("%u %u (unknown GVRP protocol ID: %u)"), xVal0, xVal1, packIn->ProtocolID);
		return str;
	}

	switch(xLenIn){
		case 2:
			str = _T("n.a.");
			break;
		case 3:
			str.Format(_T("%u"), xVal0);
			break;
		case 4:
			wVal = (WORD)xVal0;
			wVal = wVal<<8;
			wVal |= (WORD)xVal1;
			str.Format(_T("%u"), wVal);
			break;
		default:
			str.Format(_T("%u %u (invalid GVRP attribute length)"), xVal0, xVal1);
			break;
	}

	return str;
}

CString MGxRPDecoder::GetAttributeValueGMRP(MGxRPPacket* packIn, BYTE xLenIn, BYTE xVal0, BYTE xVal1)
{
	CString str;

	str.Format(_T("%u %u (GMRP protocol ID: %u)"), xVal0, xVal1, packIn->ProtocolID);
	return str;
}

CString MGxRPDecoder::GetAttributeValue(MGxRPPacket* packIn, BYTE xLenIn, BYTE xVal0, BYTE xVal1)
{
	CString str;

	if(packIn==NULL){
		str.Empty();
		return str;
	}

	switch(packIn->ProtocolType){
		case __gxrpGvrp :
			return GetAttributeValueGVRP(packIn, xLenIn, xVal0, xVal1);

		case __gxrpGmrp :
			return GetAttributeValueGMRP(packIn, xLenIn, xVal0, xVal1);

		default :
			str.Format(_T("%u %u (invalid protocol type: %u)"), xVal0, xVal1, packIn->ProtocolType);
			return str;
	}
}

//---------------------------------------------------------------------------
